/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package IO;

import java.awt.Graphics2D;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;

/**
 *
 * @author avallejo
 */
public class ImageHelper {

    public ImageHelper()
        {
    
        }
  /*  
    public void getMirroredImage(String pathString)
    {
        BufferedImage simg = null;
        
        //File object 
        File f = null;
        
        try
        {
         f = new File(pathString);
         simg = ImageIO.read(f);
         }
        catch(Exception e)
        {
            System.out.println("Error " + e);
            
        }
        
        //get source image dimension
        int width = simg.getWidth();
        int height = simg.getHeight();
        
        //Buffered image for mirror
        BufferedImage mimg = new BufferedImage(width*2, height*2,BufferedImage.TYPE_INT_ARGB);
        
        //create mirror image 
        for(int y=0; y< height ; y++)
        {
            for(int lx=0, rx=width*2-1; lx<width; lx++, rx-- ) 
            {
                 //lx starts from left side of the image
                //rx starts from the right side of the image
                
                //get source pixel value
                int p = simg.getRGB(lx, y);
                
                //set mirror image pixel value - both left and right
                mimg.setRGB(lx,y,p);
                mimg.setRGB(rx,y,p);
                
            }
         }
        
        //save mirrored image
        try
        {
            Image reversedImg = (Image)mimg;
            //f = new File("C:\\Users\\avallejo\\Documents\\NetBeansProjects\\Programathon\\trunk\\Programathon\\Carpeta Maestra\\Personajes\\P-Humano\\output.png");
            //ImageIO.write(mimg,"png",f);
            
        }
        catch(Exception e)
        {
            System.out.println("Error " + e);
            
        }
               
           
        
        
        
        
    }
*/
    
    public Image getMirroredImage(String pathString, boolean reverseNeeded)
    {
                    
        BufferedImage simg = null;
        
        //File object 
        File f = null;
        
        if(!reverseNeeded)
        {
            try{
            f = new File(pathString);
            simg = ImageIO.read(f);
               }catch(Exception e)
           {
               System.out.println("Error " + e);
               return null;
           }
        }
        
        
        try
        {
         f = new File(pathString);
         simg = ImageIO.read(f);
         }
        catch(Exception e)
        {
            System.out.println("Error " + e);
            
        }
        
        //get source image dimension
        int width = simg.getWidth();
        int height = simg.getHeight();
        
        //Buffered image for mirror
        BufferedImage mimg = new BufferedImage(width, height,BufferedImage.TYPE_INT_ARGB);//(width*2, height*2,BufferedImage.TYPE_INT_ARGB);
        
        //create mirror image 
        for(int y=0; y< height ; y++)
        {
            for(int lx=0, rx=width-1; lx<width; lx++, rx-- ) 
            {
                 //lx starts from left side of the image
                //rx starts from the right side of the image
                
                //get source pixel value
                int p = simg.getRGB(lx, y);
                
                //set mirror image pixel value - both left and right
                //mimg.setRGB(lx,y,p);
                mimg.setRGB(rx,y,p);
                
            }
         }
        
        //save mirrored image
        try
        {
            Image reverseImg = (Image)mimg;
            
            //f = new File("C:\\Users\\avallejo\\Documents\\NetBeansProjects\\Programathon\\trunk\\Programathon\\Carpeta Maestra\\Personajes\\P-Humano\\output.png");
            //ImageIO.write(mimg,"png",f);
            
            
            return reverseImg;
        }
        catch(Exception e)
        {
            System.out.println("Error " + e);
            return null;
            
        }
    }

    
    public Image getMirroredImage(Image imgToReverse, boolean reverseNeeded)
    {
        
        BufferedImage simg = null;
        
        File f = null;
        
        if(!reverseNeeded)
        {
            return imgToReverse;
        }
        
        try
        {
         simg = toBufferedImage(imgToReverse);
         }
        catch(Exception e)
        {
            System.out.println("Error " + e);
            
        }
        
        //get source image dimension
        int width = simg.getWidth();
        int height = simg.getHeight();
        
        //Buffered image for mirror
        BufferedImage mimg = new BufferedImage(width, height,BufferedImage.TYPE_INT_ARGB);//(width*2, height*2,BufferedImage.TYPE_INT_ARGB);
        
        //create mirror image 
        for(int y=0; y< height ; y++)
        {
            for(int lx=0, rx=width-1; lx<width; lx++, rx-- ) 
            {
                 //lx starts from left side of the image
                //rx starts from the right side of the image
                
                //get source pixel value
                int p = simg.getRGB(lx, y);
                
                //set mirror image pixel value - both left and right
                //mimg.setRGB(lx,y,p);
                mimg.setRGB(rx,y,p);
                
            }
         }
        
        //save mirrored image
        try
        {
            Image reverseImg = (Image)mimg;
            return reverseImg;
        }
        catch(Exception e)
        {
            System.out.println("Error " + e);
            return null;
            
        }
               
           
        
        
        
        
    }

    
    public static BufferedImage toBufferedImage(Image img)
{
    if (img instanceof BufferedImage)
    {
        return (BufferedImage) img;
    }

    // Create a buffered image with transparency
    BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);

    // Draw the image on to the buffered image
    Graphics2D bGr = bimage.createGraphics();
    bGr.drawImage(img, 0, 0, null);
    bGr.dispose();

    // Return the buffered image
    return bimage;
}
}
